package com.axisdesign.client;

import java.util.ArrayList;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.ClickListenerCollection;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.LoadListener;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

public class FlipImage extends SimplePanel {
	Image image1;
	Image image2;
	
	static int flipImageCount=1; 
	static int curMouseOver=0;
	
	int isLoadingImages;
	boolean image1DefaultFlag;
	int flipImageID;
	boolean isImage1Visible;
	
	static ArrayList flipImages = new ArrayList();
	boolean hasOnClickAction;
	private ClickListenerCollection clickListeners;
	
	public class FlipImageListener implements LoadListener {
		public void onLoad(Widget sender) {}
		
		public void onError(Widget sender) {
			detachImages();
		}
	}
	
	public FlipImage(Image imageA,Image imageB,boolean _image1DefaultFlag) {
		flipImages.add(this);

		flipImageID=flipImageCount;
		flipImageCount++;
		
		isLoadingImages=2;
		image1=imageA;
		image2=imageB;
		
		isImage1Visible=image1DefaultFlag=_image1DefaultFlag;
		hasOnClickAction=false;
		
		//sinkEvents(Event.ONMOUSEOVER|Event.ONMOUSEMOVE|Event.ONMOUSEOUT);
		sinkEvents(Event.ONMOUSEOVER|Event.ONMOUSEOUT);
		
		image1.addLoadListener(new FlipImageListener());
		image2.addLoadListener(new FlipImageListener());
		
		init();
	}
	
	private void init() {
		//checkStatus();
		if(isImage1Visible) add(image1);
		else add(image2);
	}
		
	public void onBrowserEvent(Event event) {
		//if( (DOM.eventGetType(event) == Event.ONMOUSEOVER) || (DOM.eventGetType(event) == Event.ONMOUSEMOVE)) {
		if(DOM.eventGetType(event) == Event.ONMOUSEOVER) {
			curMouseOver = flipImageID;
			//if(image2==null) Window.alert("false");
			checkAllStatus();
		}
		else if(DOM.eventGetType(event) == Event.ONMOUSEOUT) {
			curMouseOver = 0;
			checkAllStatus();
		}
		else if(hasOnClickAction && DOM.eventGetType(event) == Event.ONCLICK) {
			if (clickListeners != null) {
				clickListeners.fireClick(this);
			}
		}
	}

	public void checkStatus() {
		if(curMouseOver == flipImageID) {
			if(isImage1Visible) {
				isImage1Visible=false;
				clear();
				add(image2);		
			}
		}
		else {
			if( ((isImage1Visible==false) && (image1DefaultFlag==true)) || getWidget()==null) {
				isImage1Visible=true;
				clear();
				add(image1);
			}
		}
	}
	
	public void detachImages() {
		clear();
		unsinkEvents(Event.ONMOUSEOVER|Event.ONMOUSEOUT|Event.ONLOAD);
		removeFromParent();
	}

	
	public void checkAllStatus() {
		int size = flipImages.size();
		FlipImage tempFlipImage;
		for(int i=0; i<size; i++)
		{
			tempFlipImage = (FlipImage)flipImages.get(i);
			tempFlipImage.checkStatus();
		}
	}
	
	public void setImage1Default(boolean _image1DefaultFlag) {
		image1DefaultFlag = _image1DefaultFlag;
		checkStatus();
	}
	
	public void addClickListener(ClickListener listener) {
		if(hasOnClickAction==false){
			sinkEvents(Event.ONCLICK);
			hasOnClickAction=true;
		}
		
		if (clickListeners == null) {
			clickListeners = new ClickListenerCollection();
	    }
	    clickListeners.add(listener);
	}
}
